// we7_wxappsample/pages/orderDishes/orderDishes.js
let app = getApp();
let http = require('../../util/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize:10,
    page:1,
    search_text:'',//搜索内容
    type_sel:0,//类型序号
    week_sel:0,//时间序号
    // type_list:[{
		// 	"id": "1",
		// 	"name": "蔬菜"
		// }, {
		// 	"id": "2",
		// 	"name": "饮料"
    // }],//菜品类型列表
    type_list:[],
    week_list:[{
      id:1,
      text:'周一'
    },{
      id:2,
      text:'周二'
    },{
      id:3,
      text:'周三'
    },{
      id:4,
      text:'周四'
    },{
      id:5,
      text:'周五'
    },{
      id:6,
      text:'周六'
    },{
      id:0,
      text:'周日'
    }],//日期列表
    // goods_list:[{
		// 	"id": "1",
		// 	"name": "蔬菜1",
		// 	"poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
		// 	"sku": "11",
		// 	"price": "21.50"
		// }, {
		// 	"id": "2",
		// 	"name": "蔬菜2",
		// 	"poster": "https:\/\/www.missweb.cn\/attachment\/images\/4\/2020\/08\/xa5KXiS1ssXAzCfjdHGsHSk13hsS5P.jpg",
		// 	"sku": "22",
		// 	"price": "12.00"
    // }],//商品列表
    goods_list:[],
  },

  //更换角色
  changeRole:function(){
    wx.navigateTo({
      url: '/zhouyuyue_yajin/pages/roleSelection/roleSelection',
    })
  },

  //添加到购物车
  addCart:function(e){
    let that = this;
    // console.log(e.currentTarget.dataset.id);
    http.post('addCart',{
      goodsId:e.currentTarget.dataset.id,
      goodsCount:1,
      send_week:that.data.week_list[that.data.week_sel].id
    },function(res){
      console.log('添加购物车',res);
      if(res.data.errno==0){
        wx.showToast({
          title: '商品已成功加入购物车',
          duration: 2000,
          icon: 'none',
        })
      }
    });
  },

  //搜索菜品
  search:function(){
    let that = this;
    http.get('search',{
      eater:app.globalData.role,
      name:app.globalData.search_text,
      page:that.data.page,
      pageSize:that.data.pageSize
    },function(res){
      console.log('搜索菜品',res)
      that.setData({
        goods_list:that.data.goods_list.concat(res.data.data)
      });
    });
  },

  //获取菜品列表
  getGoodsList:function(){
    let that = this;
    http.get('goodsByCat',{
      eater:app.globalData.role,
      category:that.data.type_list[that.data.type_sel].id,
      page:that.data.page,
      pageSize:that.data.pageSize
    },function(res){
      console.log('菜品列表',res);
      that.setData({
        goods_list:that.data.goods_list.concat(res.data.data.food)
      });
    });
  },

  //跳转搜索
  gotoSearch:function(){
    wx.navigateTo({
      url: '/zhouyuyue_yajin/pages/search/search',
    })
  },

  //获取菜品分类
  getTypeList:function(){
    let that = this;
    http.get('goods',{
      eater: app.globalData.role
    },function(res){
      // console.log('菜品分类',res);
      that.setData({
        type_list:res.data.data.category
      });
      that.getGoodsList();
    });
  },

  // 选择类型
  chooseType:function(e){
    // console.log(e);
    let that = this;
    that.setData({
      type_sel:e.currentTarget.dataset.index,
      page:1,
      goods_list:[]
    });
    that.getGoodsList();
    app.globalData.search_text = '';
    that.setData({
      search_text:''
    });
  },

  // 选择日期
  chooseWeek:function(e){
    let that = this;
    that.setData({
      week_sel:e.currentTarget.dataset.index
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getTypeList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(app.globalData.search_text!=''){
      that.setData({
        search_text:app.globalData.search_text,
        goods_list:[]
      });
      that.data.page = 1;
      that.search();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if(that.data.goods_list.length==that.data.pageSize*that.data.page){
      that.setData({
        page:that.data.page + 1
      });
      if(app.globalData.search_text!=''){
        that.search();
      } else {
        that.getGoodsList();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})